<?php
set_time_limit(0);
header("Content-type:text/html;charset=utf-8");
include_once dirname(dirname(__FILE__)) . '/nn_logic/nl_common.func.php';
include_once dirname(dirname(__FILE__)) . '/mgtv_v2/mgtv_init.php';
define('XIAOLE_MOIVE_IMPORT_SP', 'jscn_sihua');
$dc = nl_get_dc(array(
		'db_policy' => NL_DB_WRITE,
		'cache_policy' => NP_KV_CACHE_TYPE_MEMCACHE
));

$sql = "select temp.* from(SELECT count(*) AS group_count,task.* FROM	nns_mgtvbk_c2_task AS task " .
        " LEFT JOIN nns_vod_media AS media ON media.nns_id = task.nns_ref_id  " .
        " WHERE	task.nns_org_id = 'jscn_sihua' AND task.nns_type = 'media' AND task.nns_action != 'destroy' " .
        " AND task.nns_status = '0' AND task.nns_epg_status = '99' and task.nns_src_id is not null  and task.nns_src_id !='' GROUP BY task.nns_src_id) as temp where temp.group_count>1 limit 10";
echo $sql."<hr/>";       
$result = nl_query_by_db($sql, $dc->db());
if(!$result)
{
    echo $sql;die;
}
if(!is_array($result) || empty($result))
{
    echo "null";die;
}
$arr_del = array();
foreach ($result as $value)
{
    $sql_media="SELECT task.nns_id as task_id,media.nns_id as media_id,media.nns_content_id as media_content_id,task.nns_name as task_name FROM nns_mgtvbk_c2_task AS task " .
        " LEFT JOIN nns_vod_media AS media ON media.nns_id = task.nns_ref_id " .
        " WHERE	task.nns_org_id = 'jscn_sihua' and task.nns_src_id='{$value['nns_src_id']}' AND task.nns_type = 'media' AND task.nns_action != 'destroy' " .
        " AND task.nns_status = '0' AND task.nns_epg_status = '99' and task.nns_src_id is not null  and task.nns_src_id !='' ";
    $result_c2_media = nl_query_by_db($sql_media, $dc->db());
    if(!$result_c2_media)
    {
        echo $sql_media;die;
    }
    if(!is_array($result_c2_media) || empty($result_c2_media))
    {
        continue;
    }
    $flag=false;
    $arr_temp_del = null;
    foreach ($result_c2_media as $media_value)
    {
        if(!isset($media_value['media_content_id']) || strlen($media_value['media_content_id']) <1)
        {
            continue;
        }
        $str_temp = substr($media_value['media_content_id'], 0,8);
        if($str_temp == 'JSB12031')
        {
            $flag = true;
        }
        else
        {
            $arr_temp_del[] = array(
                'task_id'=>$media_value['task_id'],
                'media_id'=>$media_value['media_id'],
                'task_name'=>$media_value['task_name'],
                'media_content_id'=>$media_value['media_content_id'],
            );
        }
    }
    if($flag && !empty($arr_temp_del) && is_array($arr_temp_del))
    {
        $arr_del = array_merge($arr_del,$arr_temp_del);
    }
}
if(empty($arr_del) || !is_array($arr_del))
{
    echo "NO data";die;
}
$date=date("Y-m-d H:i:s");
foreach ($arr_del as $del_value)
{
    echo "--------mediaID[{$del_value['task_id']}]--------taskID[{$del_value['media_id']}]-----------contentID[{$del_value['media_content_id']}]----------".$del_value['task_name']."<hr>";
//     continue;
    $sql_media_ex = "update nns_vod_media set nns_deleted='1',nns_create_time='{$date}',nns_modify_time='{$date}' where nns_id='`{$del_value['media_id']}' ";
    $sql_task_ex = "update nns_mgtvbk_c2_task set nns_action='destroy',nns_create_time='{$date}',nns_modify_time='{$date}',nns_status='0',nns_epg_status='97' where nns_id='{$del_value['task_id']}' and nns_org_id='jscn_sihua' ";
    $ret_media_ex = nl_execute_by_db($sql_media_ex, $dc->db());
    if(!$ret_media_ex)
    {
        echo $sql_media_ex;die;
    }
    $ret_task_ex = nl_execute_by_db($sql_task_ex, $dc->db());
    if(!$ret_task_ex)
    {
        echo $sql_task_ex;die;
    }
}
echo "OK";
